//
//  ZLVisitorsView.swift
//  ZLWeibo
//
//  Created by zhanglongtao on 16/12/16.
//  Copyright © 2016年 hanju001. All rights reserved.
//

import UIKit

class ZLVisitorsView: UIView {

    var visitorInfo: [String: String]?{
        
        didSet{
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else{
                    
                    return
            }
            
            tipLabel.text = message
            
            //首页
            if imageName == ""{
                
                startAnimation()
                
                toWatchOther.isHidden = false
                
                registerButton.isHidden = true
                
                loginButton.isHidden = true
                
                return
            }
            
            iconView.image = UIImage(named: imageName)
            
            //其它访客视图不需要显示小房子
            houseiConView.isHidden = true
            maskIconView.isHidden = true
            
        }
    }
    //构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    ///设置旋转动画
    func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        //动画完成不删除
        animation.isRemovedOnCompletion = false
        //设置动画图层
        iconView.layer.add(animation, forKey: nil)
    }
    // MARK -- 设置访客视图
    //使用字典设置访客视图的信息
    //提示如果是首页imageName= ""
    
    // MARK -- 私有控件
   
    
    fileprivate lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //不要使用maskView
     fileprivate lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    fileprivate lazy var houseiConView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    fileprivate lazy var tipLabel: UILabel = UILabel.hm_label(withText: "关注一些人，回这里看看有什么惊喜", fontSize: 14, textColor: UIColor.darkGray)
    lazy var registerButton: UIButton = UIButton.hm_textButtonAndBack("注册", fontSize: 16, normalColor: UIColor.orange, highlight: UIColor.orange, backGroundImage: "common_button_white_disable")
    lazy var loginButton: UIButton = UIButton.hm_textButtonAndBack("登录", fontSize: 16, normalColor: UIColor.darkGray, highlight: UIColor.darkGray, backGroundImage: "common_button_white_disable")
    lazy var toWatchOther: UIButton = UIButton.hm_textButtonAndBack("去关注", fontSize: 16, normalColor: UIColor.orange, highlight: UIColor.orange, backGroundImage: "common_button_white_disable")
  

}
extension ZLVisitorsView{
    
    func setupUI() {
        
        backgroundColor = UIColor.hm_color(withHex: 0xEDEDED)//如果能使用颜色就不要使用图片
        //添加控件
        
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseiConView)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        addSubview(toWatchOther)
        //文本居中
        tipLabel.textAlignment = .center
        //取消autoresizing
        for i in subviews {
            
            i.translatesAutoresizingMaskIntoConstraints = false
        }
        let margin: CGFloat = 20.0
        //自动布局
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -60))
        
        addConstraint(NSLayoutConstraint(item: houseiConView, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseiConView, attribute: .centerY, relatedBy: .equal, toItem: iconView, attribute: .centerY, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .top, relatedBy: .equal, toItem: iconView, attribute: .bottom, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 236))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .left, relatedBy: .equal, toItem: tipLabel, attribute: .left, multiplier: 1.0, constant: margin))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1.0, constant: margin))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 90))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .right, relatedBy: .equal, toItem: tipLabel, attribute: .right, multiplier: 1.0, constant: -margin))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1.0, constant: margin))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .width, relatedBy: .equal, toItem: registerButton, attribute: .width, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: toWatchOther, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1.0, constant: margin))
        addConstraint(NSLayoutConstraint(item: toWatchOther, attribute: .centerX, relatedBy: .equal, toItem: tipLabel, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: toWatchOther, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 90))
        /**只有首页有  县隐藏了*/
        toWatchOther.isHidden = true
        
        
        //Views 定义VFL中控件名称和实际名称映射关系
        //metrics 定义VFL用括号的指定的常数映射关系
        let metric = ["spacing": -35]
        let viewDic = ["maskIconView": maskIconView,"registerButton": registerButton] as [String : Any];
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[maskIconView]-0-|", options: [], metrics: nil, views: viewDic))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[maskIconView]-(spacing)-[registerButton]", options: [], metrics: metric, views: viewDic))
        
        // 25-使用字典设置访客视图的信息
    }
}
